package com.define.ssh.action.terminalparam;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.StringUtils;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.common.JsonUtil;
import com.define.ssh.model.Terminal;
import com.define.ssh.model.Terminalparam;
import com.define.ssh.model.TerminalparamType;
import com.define.ssh.service.TerminalparamService;
import com.define.ssh.util.Pager.Order;
import com.sxit.ecwx.core.utils.Formatu;

/**
 *  
 * 
 */
@ParentPackage("admin")
public class TerminalparamAction extends BaseAdminAction {
	
	private static final long serialVersionUID = 4L;
	/** 页面传递参数过来*/
	protected int pageSize;
	protected int pageNum;
	
	private List<Terminal> terminalList;
	private List<TerminalparamType> terminalparamTypeList;

	@Resource(name = "terminalparamServiceImpl")
	private TerminalparamService terminalparamService;
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		
		try {
		 DetachedCriteria dt = DetachedCriteria.forClass(Terminal.class);	
		 terminalList=terminalparamService.findByCriteria(dt);	
		 for (int i = 0; i < terminalList.size(); i++) {
			 Terminal ss=terminalList.get(i);
			 System.out.println(ss.getName());
		}
		 
		 DetachedCriteria dt1 = DetachedCriteria.forClass(TerminalparamType.class);	
		 terminalparamTypeList=terminalparamService.findByCriteria(dt1);	
	
		HashMap<String, Object> searchMap = new HashMap<String, Object>();
		pager.setOrder(Order.desc);
		pager.setOrderBy("updateTime");
		pager.setSearchMap(searchMap);
		pager =terminalparamService.findPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return "terminalparam";
	}
	
	
	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() {
		
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String terminaltype = super.getRequest().getParameter("terminaltype");//'终端类型 1.接入模块 2.核心模块 3.接入模块',
		String netid = super.getRequest().getParameter("netid"); //'网元ID' name
		String paramid = super.getRequest().getParameter("paramid"); //'参数ID'
		String paramvalue = super.getRequest().getParameter("paramvalue"); //'参数值',
		String terminalparamTypeId = super.getRequest().getParameter("terminalparamTypeId"); 
		
		try {
		
			/* DetachedCriteria dt = DetachedCriteria.forClass(Terminalparam.class);	
			 dt.add(Restrictions.eq("paramid",Integer.valueOf(paramid)));
			 List<Terminalparam> userlist=terminalparamService.findByCriteria(dt);
			if(userlist!=null&&userlist.size()>0){
				
				response.getWriter().write("终端对象已存在请重新输入!");
				return null;
			}*/
			Date updatetime = terminalparamService.getUpdateTime("terminalparam");
			
			Terminalparam mwl=new Terminalparam();
			if(StringUtils.hasText(netid)){
				String[] nets=netid.split("_");
				mwl.setNetid(Integer.valueOf(nets[0]));
				mwl.setNetName(nets[1]);
			}
			if(StringUtils.hasText(terminalparamTypeId)){
				String[] tps=terminalparamTypeId.split("_");
				mwl.setTerminalparamTypeId(Integer.valueOf(tps[0]));
				mwl.setTerminalparamTypeParamname(tps[1]);
			}
			mwl.setTerminaltype(terminaltype);
			mwl.setParamid(Integer.valueOf(paramid));
			mwl.setParamvalue(paramvalue);
			mwl.setUpdateType(1);////更新标示 1新增  2修改  3删除
			mwl.setUpdateTime(new Date());
			terminalparamService.save(mwl);
			
			Long modfiytime=0l;
			if(updatetime==null)
				 modfiytime = 0l;
			else
			  modfiytime = updatetime.getTime() / 1000;
			   
			    byte[] by = new byte[4+4+2];
		        System.arraycopy(Formatu.int2byte(0), 0, by,0, 4); /*操作用户 */
				System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
				 short opertype=1;
				  System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
				terminalparamService.sendMsg(by, 1002);
			
			response.getWriter().write("true");
		
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return null;
	}
	
	/**
	 *详情
	 * @return
	 */
	public String TerminalparamInfo() {
		
		try {
			String id = super.getRequest().getParameter("id");
			 DetachedCriteria dt = DetachedCriteria.forClass(Terminalparam.class);	
			 dt.add(Restrictions.eq("id",Integer.valueOf(id)));
			 List<Terminalparam> userlist=terminalparamService.findByCriteria(dt);
			 if(userlist!=null&&userlist.size()>0){
				 Terminalparam mwl= userlist.get(0);
			     return ajax(JsonUtil.beanToJson(mwl));
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/****
	 * 修改
	 */
	public String update() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String id = super.getRequest().getParameter("id"); 
		String terminaltype = super.getRequest().getParameter("terminaltype");//'终端类型 1.接入模块 2.核心模块 3.接入模块',
		String netid = super.getRequest().getParameter("netid"); //'网元ID' name
		String paramid = super.getRequest().getParameter("paramid"); //'参数ID'
		String paramvalue = super.getRequest().getParameter("paramvalue"); //'参数值',
		String terminalparamTypeId = super.getRequest().getParameter("terminalparamTypeId"); 
		
		 DetachedCriteria dt = DetachedCriteria.forClass(Terminalparam.class);	
		 dt.add(Restrictions.eq("id",Integer.valueOf(id)));
		 List<Terminalparam> userlist=terminalparamService.findByCriteria(dt);
		 Date updatetime = terminalparamService.getUpdateTime("terminalparam");
			
		 if(userlist!=null&&userlist.size()>0){
			 Terminalparam mwl= userlist.get(0);
			 
				if(StringUtils.hasText(netid)){
					String[] nets=netid.split("_");
					mwl.setNetid(Integer.valueOf(nets[0]));
					mwl.setNetName(nets[1]);
				}
				if(StringUtils.hasText(terminalparamTypeId)){
					String[] tps=terminalparamTypeId.split("_");
					mwl.setTerminalparamTypeId(Integer.valueOf(tps[0]));
					mwl.setTerminalparamTypeParamname(tps[1]);
				}
				mwl.setTerminaltype(terminaltype);
				mwl.setParamid(Integer.valueOf(paramid));
				mwl.setParamvalue(paramvalue);
			 mwl.setUpdateType(2);////更新标示 1新增  2修改  3删除
			 mwl.setUpdateTime(new Date());
			
			 terminalparamService.update(mwl);
			    Long modfiytime = updatetime.getTime() / 1000;
			    byte[] by = new byte[4+4+2];
		        System.arraycopy(Formatu.int2byte(0), 0, by,0, 4); /*操作用户 */
				System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
				 short opertype=2;
				  System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
				terminalparamService.sendMsg(by, 1002);
			 response.getWriter().write("true");
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/****
	 * 删除
	 */
	public String delete() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String id = super.getRequest().getParameter("id");
		 DetachedCriteria dt = DetachedCriteria.forClass(Terminalparam.class);	
		 dt.add(Restrictions.eq("id",Integer.valueOf(id)));
		 List<Terminalparam> userlist=terminalparamService.findByCriteria(dt);
		 if(userlist!=null&&userlist.size()>0){
			 Terminalparam mwl= userlist.get(0);
			 terminalparamService.delete(mwl);
			// mwl.setUpdateType(3);////更新标示 1新增  2修改  3删除
			// mwl.setUpdateTime(new Date());
			// terminalparamService.update(mwl);
			 response.getWriter().write("true");
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public List<Terminal> getTerminalList() {
		return terminalList;
	}


	public void setTerminalList(List<Terminal> terminalList) {
		this.terminalList = terminalList;
	}


	public List<TerminalparamType> getTerminalparamTypeList() {
		return terminalparamTypeList;
	}


	public void setTerminalparamTypeList(
			List<TerminalparamType> terminalparamTypeList) {
		this.terminalparamTypeList = terminalparamTypeList;
	}
	
	
}